import functionnodes.FunctionNode;
import data.Value;

/**
 * @author loaner
 */
class functionnodes.FunctionNodeSetc extends FunctionNode {
	
	
	
	function FunctionNodeSetc() {
		super();
		funcname = "setc";
		type = SPECIALFUNCTION;
	}
	/*
	public function reduceValues( scope:Object, args:Array):Value
	{
		//first arg is the clip whose property we're setting,
		//second arg is the name of the property,
		//third arg is the value to put into it
		var mc:MovieClip = args[0].raw();
		trace("In setc, mc = " + mc );
		var propname:String = args[1].raw();
		
		mc[ propname ] = args[2].raw();
		return args[2];
	}
*/

	public function reduceXMLtoValue( scope:Object, XMLargs:Array ):Value
	{
		var returnvalue:Value;
		
		//args[0] is the clip! we need to evaluate that XML to get a Value object
		//whose raw() will give us the movieclip.
		
		var mcval:Value = Evaluator.convertXMLtoValue( XMLargs[0], scope );
		
		var mc:MovieClip = mcval.raw();
		
		//as for the rest:
		
		
		var args:XMLNode = XMLargs[1];
		
		trace("XMLargs[1] is " + args + " and XMLargs[1].nodeName = " + args.nodeName );
		
		for( var i : Number = 0; i < args.childNodes.length; i++)
		{
			var pair:XMLNode = args.childNodes[ i ];
			trace("firstpair is " + pair );
			trace("first half is " + pair.childNodes[ 0 ]);
			trace("second half is " + pair.childNodes[ 1 ]);
			var firstval:Value = Evaluator.convertXMLtoValue( pair.childNodes[ 0 ], scope );
			var secondval:Value = Evaluator.convertXMLtoValue( pair.childNodes[ 1 ], scope );
			trace("firstval.raw() = " + firstval.raw() );
			trace("secondval.raw() = " + secondval.raw() );
			mc[ firstval.raw() ] = secondval.raw();
			if( secondval.type == Value.FUNCTIONNODE ) // aha! we're trying to attach a function to a clip.
			//which is okay, but the word "this" will be interesting.
			{
				var fn:FunctionNode = secondval.raw();
				fn.lambdascope["__this"] = mc;
			}
		}
		
		//a big list
		//containing pair-lists
		/*
		 * <list>
		 * 		<list>
		 * 			<q>propertyname</q>
		 * 			<symbol>newvalue</symbol>
		 * 		</list>
		 * 		<list>
		 * 			<q>propertyname</q>
		 * 			<symbol>newvalue</symbol>
		 * 		</list>
		 * 	</list>
		 * 	*/
		 
		 
		 
		 return returnvalue;
	}

}